﻿// zxLandsatMetaDialog.cpp: 实现文件
//

#include "stdafx.h"
#include "zxLandsatMetaDialog.h"
#include "afxdialogex.h"
#include "Resource.h"

// zxLandsatMetaDialog 对话框

IMPLEMENT_DYNAMIC(zxLandsatMetaDialog, CDialogEx)

zxLandsatMetaDialog::zxLandsatMetaDialog(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_ZX_LANDSAT_METADATA, pParent)
{

}

zxLandsatMetaDialog::~zxLandsatMetaDialog()
{
}

void zxLandsatMetaDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_MFCPROPERTYGRID1, m_props);
}


BEGIN_MESSAGE_MAP(zxLandsatMetaDialog, CDialogEx)
END_MESSAGE_MAP()


// zxLandsatMetaDialog 消息处理程序


BOOL zxLandsatMetaDialog::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	CRect rect;
	GetClientRect(rect);
	HDITEM item1;
	item1.pszText = "键";
	item1.cxy = rect.Width() / 2;
	item1.mask = HDI_TEXT | HDI_WIDTH;
	m_props.GetHeaderCtrl().SetItem(0, new HDITEM(item1));
	HDITEM item2;
	item2.pszText = "值";
	item2.mask = HDI_TEXT;
	m_props.GetHeaderCtrl().SetItem(1, new HDITEM(item2));

	if (m_pDataset)
	{
		CString strTitle;
		strTitle.Format("Landsat-%s-元数据", m_pDataset->GetName());
		this->SetWindowText(strTitle);

		//PopulateMeta();
		PopulateRawMeta();
	}


	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void zxLandsatMetaDialog::PopulateRawMeta()
{
	ck::data::LandsatRawMeta* pRawMeta = m_pDataset->GetRawMetaData();
	if (pRawMeta)
	{
		z_uint32 nCount = pRawMeta->GetCount();

		ck::data::LandsatRawMetaGroup* pRawGroup = NULL;
		for (z_uint32 i = 0; i < nCount; i++)
		{
			pRawGroup = pRawMeta->GetGroup(i);
			if (pRawGroup)
			{
				const char* szName = pRawGroup->GetName();
				CMFCPropertyGridProperty* pxGroup = new CMFCPropertyGridProperty(szName);

				pRawGroup->Reset();
				while (!pRawGroup->IsEOF())
				{
					const char* szKey = pRawGroup->GetKey();
					const char* szValue = pRawGroup->GetValue();

					CMFCPropertyGridProperty* pxProperty = new CMFCPropertyGridProperty(szKey, (_variant_t)szValue, _T(""));
					pxGroup->AddSubItem(pxProperty);

					pRawGroup->Next();
				}

				m_props.AddProperty(pxGroup);
			}
		}
	}
}

void zxLandsatMetaDialog::PopulateMeta()
{
	ck::data::LandsatMeta* pMeta = m_pDataset->GetMetaData();
	if (pMeta)
	{
		z_uint32 nCount = pMeta->GetCount();
		ck::data::MetaGroup* pGroup = NULL;
		for (z_uint32 i = 0; i < nCount; i++)
		{
			pGroup = pMeta->GetGroup(i);
			if (pGroup)
			{
				const char* szName = pGroup->GetName();
				CMFCPropertyGridProperty* pxGroup = new CMFCPropertyGridProperty(szName);

				char szValue[_MAX_PATH];

				pGroup->Reset();
				while (!pGroup->IsEOF())
				{
					const char* szKey = pGroup->GetKey();
					ck::Value* pValue = pGroup->GetValue();
					memset(szValue, 0, _MAX_PATH);

					if (pValue == NULL)
					{
						//...	
					}
					else
					{
						ck::ckValueType type = pValue->GetType();
						switch (type)
						{
						case ck::ckValueTypeBool:
							break;
						case ck::ckValueTypeChar:
							break;
						case ck::ckValueTypeShort:
						{

						}
						break;
						case ck::ckValueTypeInt:
						case ck::ckValueTypeLong:
						{
							sprintf(szValue, "%d", pValue->GetInt());
						}
						break;
						case ck::ckValueTypeInt64:
							break;
						case ck::ckValueTypeFloat:
						{
							sprintf(szValue, "%f", pValue->GetFloat());
						}
						break;
						case ck::ckValueTypeDouble:
							break;
						case ck::ckValueTypeNumeric:
							break;
						case ck::ckValueTypeString:
						{
							const char* str = pValue->GetString();
							if (str)
							{
								strcpy(szValue, str);
							}
						}
						break;
						case ck::ckValueTypeDate:
						{
							const ck::ck_date_t* dt = pValue->GetDate();
							if (dt)
							{
								sprintf(szValue, "%d-%d-%d", dt->year, dt->month, dt->day);
							}
						}
						break;
						case ck::ckValueTypeTime:
						{
							const ck::ck_time_t* dt = pValue->GetTime();
							if (dt)
							{
								sprintf(szValue, "%d:%d:%fZ", dt->hour, dt->minute, dt->second);
							}
						}
						break;
						case ck::ckValueTypeDateTime:

						{
							const ck::ck_datetime_t* dt = pValue->GetDateTime();
							if (dt)
							{
								sprintf(szValue, "%d-%d-%dT%d:%d:%fZ", dt->year, dt->month, dt->day, dt->hour, dt->minute, dt->second);
							}
						}
						break;
						case ck::ckValueTypeBLOB:
							break;
						case ck::ckValueTypeGeometry:
							break;
						case ck::ckValueTypeNull:
							break;
						default:
							break;
						}
					}
					CMFCPropertyGridProperty* pxProperty = new CMFCPropertyGridProperty(szKey, (_variant_t)szValue, _T(""));
					pxGroup->AddSubItem(pxProperty);

					pGroup->Next();
				}
				m_props.AddProperty(pxGroup);
			}
		}
	}
}

void zxLandsatMetaDialog::SetDataset(ck::data::Landsat* pDataset)
{
	m_pDataset = pDataset;
}